/**************************************************************
 *
 * Copyright (c) 2008, ******, All Rights Reserved.
 *
 * 名 称:
 *      pub_acct.cpp
 * 位 置:
 *      src/biz/pub_acct.cpp
 * 描 述:
 *		系统帐号类
 * 备 注:
 *      无
 * 修 改:
 *      2008-09-21  Simon   - 创建
 *
 **************************************************************/

#include <string>
#include <iostream>
#include <exception>
#include "occi.h"
#include "pub/def.h"
#include "pub/db.h"
#include "pub/str.h"
#include "pub/log.h"
#include "biz/pub_acct.h"

using namespace std;
using namespace oracle::occi;

//----------------------------------------------
//  构造函数
//----------------------------------------------
PubAccount::PubAccount(Connection* conn)
{
	this->conn = conn;
}

//----------------------------------------------
//  析构函数
//----------------------------------------------
PubAccount::~PubAccount()
{
	this->conn = 0;
}

//----------------------------------------------
//	生成客户账户
//----------------------------------------------
void PubAccount::genCustAccount()
{
	Statement* stmt = conn->createStatement();
	ResultSet* rs = 0;
	try
	{
		Log::debug("生成客户账户...");
		while (true)
		{
			//获取客户帐号
			stmt->setSQL("select SEQ_CUSTACCTID.nextval from dual");
			rs = stmt->executeQuery();
			rs->next();
			custAccountID = rs->getString(1);
			custAccountID.lpadz(8);

			//检查客户帐号是否已经存在
			stmt->setSQL("select count(*) from AcctCust where custAccountID = :custAccountID");
			stmt->setString(1, custAccountID);
			rs = stmt->executeQuery();
			rs->next();
			int count = rs->getInt(1);
			if (count <= 0)
			{
				Log::debug("客户账户: [%s]", custAccountID.c_str());
				break;
			}
		}
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
	}
	catch (exception& e)
	{
		Log::error("生成客户账户出错. %s", e.what());
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		throw;
	}
}

//----------------------------------------------
//  生成交易账户
//----------------------------------------------
void PubAccount::genTransAccount()
{
	Statement* stmt = conn->createStatement();
	ResultSet* rs = 0;
	try
	{
		Log::debug("生成交易账户...");
		while (true)
		{
			//获取交易帐号
			if (invstType == "0")
			{
				//机构投资者
				stmt->setSQL("select SEQ_TRANSACCTIDINST.nextval from dual");
			}
			else
			{
				//个人投资者
				stmt->setSQL("select SEQ_TRANSACCTIDINDV.nextval from dual");
			}
			rs = stmt->executeQuery();
			rs->next();
			transAccountID = rs->getString(1);

			//检查交易帐号是否已经存在
			stmt->setSQL("select count(*) from AcctTrans where transAccountID = :transAccountID");
			stmt->setString(1, transAccountID);
			rs = stmt->executeQuery();
			rs->next();
			int count = rs->getInt(1);
			if (count <= 0)
			{
				Log::debug("交易账户: [%s]", transAccountID.c_str());
				break;
			}
		}
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
	}
	catch (exception& e)
	{
		Log::error("生成交易账户出错. %s", e.what());
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		throw;
	}
}

//----------------------------------------------
//  生成基金帐号
//----------------------------------------------
void PubAccount::genTAAccount()
{
	cout << "genTAAccount!";
}
